/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.crossriver.crgis.database.service.messaging;

import com.crossriver.crgis.database.service.Application_C_of_OLocalServiceUtil;
import com.crossriver.crgis.database.service.Application_C_of_OServiceUtil;
import com.crossriver.crgis.database.service.Bank_Payment_SlipLocalServiceUtil;
import com.crossriver.crgis.database.service.Bank_Payment_SlipServiceUtil;
import com.crossriver.crgis.database.service.ClpSerializer;
import com.crossriver.crgis.database.service.Company_Contact_PersonLocalServiceUtil;
import com.crossriver.crgis.database.service.Company_Contact_PersonServiceUtil;
import com.crossriver.crgis.database.service.Contact_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Contact_InformationServiceUtil;
import com.crossriver.crgis.database.service.DocumentsLocalServiceUtil;
import com.crossriver.crgis.database.service.DocumentsServiceUtil;
import com.crossriver.crgis.database.service.Held_PlotsLocalServiceUtil;
import com.crossriver.crgis.database.service.Held_PlotsServiceUtil;
import com.crossriver.crgis.database.service.History_Of_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.History_Of_InformationServiceUtil;
import com.crossriver.crgis.database.service.Individual_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Individual_InformationServiceUtil;
import com.crossriver.crgis.database.service.Organisation_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Organisation_InformationServiceUtil;
import com.crossriver.crgis.database.service.Property_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Property_InformationServiceUtil;
import com.crossriver.crgis.database.service.Supporting_DocumentLocalServiceUtil;
import com.crossriver.crgis.database.service.Supporting_DocumentServiceUtil;
import com.crossriver.crgis.database.service.TransactionsLocalServiceUtil;
import com.crossriver.crgis.database.service.TransactionsServiceUtil;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;

/**
 * @author Brian Wing Shun Chan
 */
public class ClpMessageListener implements MessageListener {
	public static final String SERVLET_CONTEXT_NAME = ClpSerializer.SERVLET_CONTEXT_NAME;

	public void receive(Message message) {
		try {
			doReceive(message);
		}
		catch (Exception e) {
			_log.error("Unable to process message " + message, e);
		}
	}

	protected void doReceive(Message message) throws Exception {
		String command = message.getString("command");
		String servletContextName = message.getString("servletContextName");

		if (command.equals("undeploy") &&
				servletContextName.equals(SERVLET_CONTEXT_NAME)) {
			Application_C_of_OLocalServiceUtil.clearService();

			Application_C_of_OServiceUtil.clearService();
			Supporting_DocumentLocalServiceUtil.clearService();

			Supporting_DocumentServiceUtil.clearService();
			DocumentsLocalServiceUtil.clearService();

			DocumentsServiceUtil.clearService();
			Individual_InformationLocalServiceUtil.clearService();

			Individual_InformationServiceUtil.clearService();
			Organisation_InformationLocalServiceUtil.clearService();

			Organisation_InformationServiceUtil.clearService();
			Contact_InformationLocalServiceUtil.clearService();

			Contact_InformationServiceUtil.clearService();
			Company_Contact_PersonLocalServiceUtil.clearService();

			Company_Contact_PersonServiceUtil.clearService();
			Property_InformationLocalServiceUtil.clearService();

			Property_InformationServiceUtil.clearService();
			Held_PlotsLocalServiceUtil.clearService();

			Held_PlotsServiceUtil.clearService();
			History_Of_InformationLocalServiceUtil.clearService();

			History_Of_InformationServiceUtil.clearService();
			TransactionsLocalServiceUtil.clearService();

			TransactionsServiceUtil.clearService();
			Bank_Payment_SlipLocalServiceUtil.clearService();

			Bank_Payment_SlipServiceUtil.clearService();
		}
	}

	private static Log _log = LogFactoryUtil.getLog(ClpMessageListener.class);
}